package com.lipiao.makerandroid.View.Activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.MenuItem;
import android.view.View;
import android.widget.PopupMenu;
import android.widget.Toast;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.lipiao.makerandroid.Bean.RespondBean.ReadBean;
import com.lipiao.makerandroid.R;
import com.lipiao.makerandroid.Utils.HttpUtil;
import com.lipiao.makerandroid.View.Adapter.IssueAdapter;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import org.json.JSONArray;
import org.json.JSONException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class IssueActivity extends AppCompatActivity {

    static final String TAG = "IssueActivity";

    String userNumber;

    private List<ReadBean> mList = new ArrayList<>();

    RecyclerView mRecyclerView;

    RefreshLayout refreshLayout;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_issue);
        mRecyclerView = findViewById(R.id.rv_issue);
        System.out.println(mRecyclerView);
        refreshLayout = findViewById(R.id.srl_issue);
        userNumber = getIntent().getStringExtra("userNumber");
        initView();
        fetchData();
    }

    public void initView(){
//        refreshLayout.setReboundDuration(200);//回弹动画

        refreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                refreshlayout.finishRefresh(500);
                fetchData();
            }
        });
        mRecyclerView.setHasFixedSize(true);

        //平常的水平一个item布局的流
        LinearLayoutManager layoutManager = new LinearLayoutManager(getBaseContext());
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        mRecyclerView.setLayoutManager(layoutManager);
    }

    public void fetchData(){
        Call<ResponseBody> callInfo = HttpUtil.getUserService().getReadByUserNumber(userNumber);
        callInfo.enqueue(new Callback<ResponseBody>() {
            @Override
            public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
                try{
                    String str = response.body().string();
                    JSONArray jsonArray = new JSONArray(str);
                    Gson gson = new Gson();
                    System.out.println(jsonArray.toString());
                    List<ReadBean> readBeanList = gson.fromJson(jsonArray.toString(),new TypeToken<List<ReadBean>>(){}.getType());
                    mList=readBeanList;
                    initData(readBeanList);
                } catch (JSONException exception) {
                    exception.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFailure(Call<ResponseBody> call, Throwable t) {

            }
        });
    }

    public void initData(List<ReadBean> readBeanList){
        IssueAdapter issueAdapter = new IssueAdapter(readBeanList);
        issueAdapter.setOnItemClickListener(new IssueAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                Intent intent = new Intent(getApplicationContext(), ReadActivity.class);
//                System.out.println(mList.get(position).getReadText());
                intent.putExtra("text", mList.get(position).getReadText());
                intent.putExtra("userNo", mList.get(position).getUserNumber());
                intent.putExtra("userNumber", userNumber);
                intent.putExtra("readId", mList.get(position).getReadId());
                System.out.println("userNumber:"+userNumber);
                startActivity(intent);
            }


            //长按
            @Override
            public void onItemLongClick(View view, int position) {
               myPopupMenu(view,userNumber,mList.get(position).getReadId());
            }
        });

        mRecyclerView.setAdapter(issueAdapter);
    }
    private void myPopupMenu(View v,String userNumber,Integer readId) {
        //定义PopupMenu对象
        PopupMenu popupMenu = new PopupMenu(v.getContext(), v);
        //设置PopupMenu对象的布局
        popupMenu.getMenuInflater().inflate(R.menu.toolbar_menu2, popupMenu.getMenu());
        //设置PopupMenu的点击事件
        popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(MenuItem item) {
//                Toast.makeText(getContext(), item.getItemId()+"", Toast.LENGTH_SHORT).show();
                String str = (String) item.getTitle();
                if (str.equals("取消发布")) {
//                    Toast.makeText(getActivity(), "点击了----" + item.getTitle(), Toast.LENGTH_SHORT).show();
                    Call<ResponseBody> callCollectionList = HttpUtil.getUserService().deleteMyRead(userNumber, readId);
                    callCollectionList.enqueue(new Callback<ResponseBody>() {
                        @Override
                        public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
                            String strBack = null;
                            try {
                                strBack = response.body().string();
                                System.out.println(strBack);
                            } catch (IOException e) {
                                e.printStackTrace();
                            }
                            if (strBack.equals("deleteSuccess")) {
                                Toast.makeText(IssueActivity.this, "删除发布成功！", Toast.LENGTH_SHORT).show();
                                fetchData();
                            } else {
                                Toast.makeText(IssueActivity.this, "删除发布失败！", Toast.LENGTH_SHORT).show();
                            }
                        }

                        @Override
                        public void onFailure(Call<ResponseBody> call, Throwable t) {

                        }
                    });
                } else if (str.equals("分享至")) {
                    Toast.makeText(IssueActivity.this, "分享成功！", Toast.LENGTH_SHORT).show();
                }
                return true;
            }
        });
        //显示菜单
        popupMenu.show();
    }

}
